<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('includes/functions/module_functions.php');
class ot_volume_discount {

	function ot_volume_discount() {
		$this->module_key = 'MODULE_ORDER_TOTAL_VOLUME_DISCOUNT';
		$this->code = 'ot_volume_discount';
		$this->enabled = ((MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_SORT_ORDER;
	}

	function process($params, &$checkout_component)
	{
		$cart = &$checkout_component->cart;
		$cart_prices = &$checkout_component->cart_prices;

		$cartVolumeDiscountPrice = 0.0;
		$cartVolumeDiscountTax = 0.0;

		$sz = sizeof($cart);
		for ($i=0; $i< $sz; $i++)
		{
			$price = (double)$cart[$i]['cart_price'];
			$discount_category_id = (int)$cart[$i]['discount_categories_id'];
			if ($discount_category_id == (int)MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_CATEGORY_ID)
			{
				$attributes = $cart[$i]['cart_options'];
				$sz1 = sizeof($attributes);
				$attributeIds = '';
				for ($j=0; $j<$sz1; $j++)
				{
					$attributeIds = '+' . $attributes[$j]['id'] . '_' . $attributes[$j]['values_id'];
				}
				$discountQuantity = $this->getDiscountCategoryQuantities($cart, $discount_category_id, $attributeIds);

				if ($discountQuantity >= (int)MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_QUANTITY) 
				{
					$volume_discount = ($price * (int)$cart[$i]['cart_qty']) * ((double)MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_PERCENTAGE_OFF / 100) * -1.0;
					$cartVolumeDiscountPrice += $volume_discount;
					$cartVolumeDiscountTax += ($cart[$i]['tax_rate']/100) * $volume_discount;
				}
			}
		}
		
		if ($cartVolumeDiscountPrice != 0) {
			$cart_prices[$this->code] = array('value' => $cartVolumeDiscountPrice, 'title' => 'Volume Discount:', 'class' => $this->code);
			$cart_prices['ot_tax']['value'] += $cartVolumeDiscountTax;
		}
	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Display Volume Discount at Checkout', 'MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_STATUS', 'false', 'Do you want to display the special discount?', '6', '1','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_SORT_ORDER', '60', 'Sort order of display.', '60', '2', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Percentage Off', 'MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_PERCENTAGE_OFF', '10', 'Percentage.', '6', '2', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Starting at Quantity', 'MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_QUANTITY', '10', 'Discount is applied when this quantity is reached.', '6', '2', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Category', 'MODULE_ORDER_TOTAL_VOLUME_DISCOUNT_CATEGORY_ID', '0', 'Discount is applied to items in this discount category.', '6', '2', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}

	function getDiscountCategoryQuantities(&$cart, $discount_category_id, $attributesIds)
	{
		$quantity = 0;
		if ($discount_category_id > 0)
		{
			$sz = sizeof($cart);
			for ($i=0; $i< $sz; $i++)
			{
				if ($discount_category_id == $cart[$i]['discount_categories_id'])
				{
					$attributes = $cart[$i]['cart_options'];
					$sz1 = sizeof($attributes);
					$currAttributeIds = '';
					for ($j=0; $j<$sz1; $j++)
					{
						$currAttributeIds = '+' . $attributes[$j]['id'] . '_' . $attributes[$j]['values_id'];
					}
					if ($currAttributeIds == $attributesIds) {
						$quantity = $quantity + $cart[$i]['cart_qty'];
					}
				}
			}
		}
		return (int)$quantity;
	}
}
?>
